﻿package {
	import flash.display.Sprite;
	//混合模式类
	import flash.display.BlendMode;
	//事件类
	import flash.events.*;
	//声音类
	import flash.media.Sound;
	//混音器类
	import flash.media.SoundMixer;
	//声道类
	import flash.media.SoundChannel;
	//URLRequest类
	import flash.net.URLRequest;
	//ByteArray类
	import flash.utils.ByteArray;
	//位图类
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	//滤镜类
	import flash.filters.BlurFilter;
	import flash.filters.ColorMatrixFilter;
	//滤镜品质类
	import flash.filters.BitmapFilterQuality;
	//矩形类
	import flash.geom.Rectangle;
	//Point类(点)
	import flash.geom.Point;

	//类及变量声明
	//======================================================================================//
	public class MusicWave extends Sprite {
		// 波形图的长和宽
		private var w:int,h:int;
		//声明用来包含line和bg的Sprite
		private var Main:Sprite=new Sprite;
		//声明用来画线的Sprite
		private var line:Sprite=new Sprite;
		//声明用来放位图数据的BitmapData
		private var bmpData:BitmapData;
		//声明用来显示效果的Bitmap
		private var bmp:Bitmap;
		//声明一个ByteArray对象
		private var bArray:ByteArray=new ByteArray;
		//声明一个数组对象
		private var Ary:Array;
		//声明两个数字对象
		private var n:Number=0;
		private var c:Number=0;
		//声明一个ColorMatrix滤镜
		private var colorM:ColorMatrixFilter=new ColorMatrixFilter([0.99,0,0,0,0,0,0.99,0,0,0,0,0,0.99,0,0,0,0,0,0.99,0.02,]);

		//声明一个BlurFilter滤镜
		private var blur:BlurFilter=new BlurFilter(5,5,BitmapFilterQuality.HIGH);
		//声明一个矩形
		private var r:Rectangle;
		//声明一个点
		private var p:Point=new Point(0,0);

		//======================================================================================//
		//构造函数
		//生成实例时需要传入宽和高
		public function MusicWave(_w:int=100,_h:int=50):void {
			w=_w;
			h=_h;
			init();
		}
		//初始化
		private function init():void {
			Main.blendMode=BlendMode.ADD;
			bmpData=new BitmapData(w,h,true,0xFF0);
			bmp=new Bitmap(bmpData);
			r=new Rectangle(0,0,w,h);
			Main.addChild(bmp);
			line.x=5;
			Main.addChild(line);
			addChild(Main);
			this.addEventListener(Event.ENTER_FRAME,showWave);
		}
		//================================================》
		//时时更新波形图画线函数
		private function showWave(event:Event) {
			//将Main的内容绘制到bmpData
			bmpData.draw(Main);
			//应用滤镜
			bmpData.applyFilter(bmpData,r,p,colorM);
			bmpData.applyFilter(bmpData,r,p,blur);
			//清除绘图
			line.graphics.clear();
			//设置线条样式,颜色湖蓝,宽度1,透明度100
			var s:int=Math.floor(Math.random() * 20);
			if (s == 10) {
				var color:uint=colorRandom();
				line.graphics.lineStyle(1,color,100);
			} else {
				line.graphics.lineStyle(1,0xFF5500,100);
			}
			//将当前声音输出为ByteArray,
			//false用来制作波形图（-1-1输出）,true则用来做直方块式的图（0-1输出）
			SoundMixer.computeSpectrum(bArray,false,0);
			for (var i=0; i < 256; i+= 4) {
				//在ByteArray中读取一个32位的单精度浮点数(就是把数据流读取成浮点数)
				n=bArray.readFloat();
				//将n放大成波形图一半大小以内
				n=Math.floor(n * h / 2);
				//如果i不为0
				if (i != 0) {
					//画波形图
					line.graphics.lineTo((w - 10)/ 256 * i,n + h / 2);
				} else {
					//移动    
					line.graphics.moveTo(0,n + h / 2);
				}
			}
		}
		//================================================》
		//获取一个随机16进制数
		private function colorRandom():uint {
			var r:String=Math.floor(Math.random() * 200 + 55).toString(16);
			var g:String=Math.floor(Math.random() * 255 + 55).toString(16);
			var b:String=Math.floor(Math.random() * 255 + 55).toString(16);
			//补位
			r=r.length < 2?"0" + r:r;
			g=g.length < 2?"0" + g:g;
			b=b.length < 2?"0" + b:b;
			var color:uint=uint("0x" + r + g + b);
			return color;
		}
	}
}